<template>
  <div class="payment-container">
    <div class="package-tabs">
      <el-radio-group v-model="packageType">
        <el-radio-button label="month">月付套餐</el-radio-button>
        <el-radio-button label="year">年付套餐</el-radio-button>
      </el-radio-group>
    </div>

    <div class="package-list">
      <div v-for="plan in currentPackages" 
        :key="plan.id"
        class="package-card"
        :class="{ 'popular': plan.isPopular }"
      >
        <div class="package-header">
          <h3>{{ plan.name }}</h3>
          <div class="popular-tag" v-if="plan.isPopular">推荐</div>
          <div class="price">
            <span class="amount">￥{{ plan.price }}</span>
            <span class="period">/{{ packageType === 'month' ? '月' : '年' }}</span>
          </div>
          <div class="storage">{{ formatStorage(plan.space) }}存储空间</div>
        </div>

        <div class="features">
          <div class="feature">
            <el-icon><Upload /></el-icon>
            <span>单文件上传{{ plan.uploadLimit }}</span>
          </div>
          <div class="feature">
            <el-icon><Timer /></el-icon>
            <span>{{ plan.speed }}</span>
          </div>
          <div class="feature">
            <el-icon><Service /></el-icon>
            <span>{{ plan.support }}</span>
          </div>
        </div>

        <el-button 
          :type="plan.isPopular ? 'primary' : 'default'"
          class="buy-btn"
          @click="handlePay(plan)"
        >
          立即购买
        </el-button>
      </div>
    </div>

    <!-- 支付弹窗 -->
    <el-dialog
      v-model="showPayDialog"
      title="确认支付"
      width="360px"
      center
    >
      <div class="pay-info">
        <div class="amount">￥{{ currentPlan?.price }}</div>
        <div class="desc">{{ currentPlan?.name }}</div>
      </div>

      <div class="pay-methods">
        <div class="method" 
          v-for="method in paymentMethods" 
          :key="method.id"
          :class="{ active: payMethod === method.id }"
          @click="payMethod = method.id"
        >
          <img :src="method.icon" :alt="method.name">
        </div>
      </div>

      <template #footer>
        <el-button @click="showPayDialog = false">取消</el-button>
        <el-button 
          type="primary" 
          @click="confirmPay"
          :disabled="!payMethod"
        >
          确认支付
        </el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, computed, getCurrentInstance } from 'vue'
import { Upload, Timer, Service } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'

const { proxy } = getCurrentInstance()

const api = {
  createOrder: '/payment/create'
}

const packageType = ref('month')

const monthlyPackages = [
  {
    id: 'PKG0000001',
    name: '基础套餐',
    price: 9.90,
    space: 10240,
    uploadLimit: '100MB',
    speed: '基础传输速度',
    support: '标准支持',
    isPopular: false
  },
  {
    id: 'PKG0000002',
    name: '进阶套餐',
    price: 29.90,
    space: 51200,
    uploadLimit: '1GB',
    speed: '高速传输',
    support: '优先支持',
    isPopular: true
  },
  {
    id: 'PKG0000003',
    name: '专业套餐',
    price: 49.90,
    space: 102400,
    uploadLimit: '5GB',
    speed: '超高速传输',
    support: '24/7支持',
    isPopular: false
  }
]

const yearlyPackages = [
  {
    id: 'PKG0000004',
    name: '基础年套餐',
    price: 99.00,
    space: 10240,
    uploadLimit: '100MB',
    speed: '基础传输速度',
    support: '标准支持',
    isPopular: false
  },
  {
    id: 'PKG0000005',
    name: '进阶年套餐',
    price: 299.00,
    space: 51200,
    uploadLimit: '1GB',
    speed: '高速传输',
    support: '优先支持',
    isPopular: true
  },
  {
    id: 'PKG0000006',
    name: '专业年套餐',
    price: 499.00,
    space: 102400,
    uploadLimit: '5GB',
    speed: '超高速传输',
    support: '24/7支持',
    isPopular: false
  }
]

const currentPackages = computed(() => {
  return packageType.value === 'month' ? monthlyPackages : yearlyPackages
})

const paymentMethods = [
  {
    id: 'alipay',
    name: '支付宝支付',
    icon: '/src/assets/images/alipay.svg'
  },
  {
    id: 'wechat',
    name: '微信支付',
    icon: '/src/assets/images/wechat.svg'
  }
]

const showPayDialog = ref(false)
const currentPlan = ref(null)
const payMethod = ref('')

const formatStorage = (mb) => {
  if (mb >= 1024) {
    return (mb / 1024).toFixed(0) + 'GB'
  }
  return mb + 'MB'
}

const handlePay = (plan) => {
  currentPlan.value = plan
  payMethod.value = ''
  showPayDialog.value = true
}

const confirmPay = async () => {
  if (!currentPlan.value) {
    ElMessage.warning('请先选择套餐')
    return
  }
  if (!payMethod.value) {
    ElMessage.warning('请选择支付方式')
    return
  }

  const params = {
    packageId: currentPlan.value.id,
    paymentMethod: payMethod.value,
    amount: currentPlan.value.price
  }

  try {
    const response = await proxy.Request({
      url: api.createOrder,
      method: 'post',
      params: params,
      dataType: 'json'
    })

    if (response.code === 200) {
      ElMessage.success('订单创建成功，套餐已激活')
      showPayDialog.value = false
      window.location.reload()
    } else {
      ElMessage.error(response.message || '支付失败，请稍后重试')
    }
  } catch (error) {
    console.error('支付请求失败:', error)
    ElMessage.error(error.response?.data?.message || '支付失败，请稍后重试')
  }
}
</script>

<style lang="scss" scoped>
.payment-container {
  padding: 40px;
  max-width: 1200px;
  margin: 0 auto;

  .package-tabs {
    text-align: center;
    margin-bottom: 40px;
  }

  .package-list {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
    gap: 24px;
  }

  .package-card {
    background: #fff;
    border-radius: 12px;
    padding: 24px;
    text-align: center;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
    transition: transform 0.3s;
    border: 2px solid transparent;

    &:hover {
      transform: translateY(-4px);
    }

    &.popular {
      border-color: #f701ff;
    }

    .package-header {
      position: relative;
      margin-bottom: 24px;

      h3 {
        font-size: 20px;
        color: #333;
        margin: 0 0 16px;
      }

      .popular-tag {
        position: absolute;
        top: -12px;
        right: -12px;
        background: #f701ff;
        color: #fff;
        padding: 4px 12px;
        border-radius: 12px;
        font-size: 12px;
      }

      .price {
        margin-bottom: 8px;

        .amount {
          font-size: 32px;
          font-weight: 600;
          color: #f701ff;
        }

        .period {
          font-size: 14px;
          color: #666;
        }
      }

      .storage {
        font-size: 16px;
        color: #333;
        font-weight: 500;
      }
    }

    .features {
      display: flex;
      flex-direction: column;
      gap: 16px;
      margin-bottom: 24px;
      padding: 16px;
      background: #f8f9fa;
      border-radius: 8px;

      .feature {
        display: flex;
        align-items: center;
        gap: 12px;

        .el-icon {
          font-size: 20px;
          color: #f701ff;
        }

        span {
          font-size: 14px;
          color: #666;
          text-align: left;
        }
      }
    }

    .buy-btn {
      width: 100%;
      height: 40px;
      border-radius: 20px;
      font-size: 16px;

      &.el-button--primary {
        background: #f701ff;
        border-color: #f701ff;

        &:hover {
          opacity: 0.9;
        }
      }
    }
  }
}

.pay-info {
  text-align: center;
  margin-bottom: 24px;

  .amount {
    font-size: 32px;
    font-weight: 600;
    color: #f701ff;
    margin-bottom: 8px;
  }

  .desc {
    color: #666;
  }
}

.pay-methods {
  display: flex;
  justify-content: center;
  gap: 16px;
  margin-bottom: 24px;

  .method {
    width: 120px;
    height: 40px;
    border: 2px solid #eee;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.3s;

    img {
      height: 24px;
    }

    &:hover {
      border-color: #f701ff;
    }

    &.active {
      border-color: #f701ff;
      background: rgba(247, 1, 255, 0.05);
    }
  }
}

:deep(.el-dialog__footer) {
  text-align: center;
  
  .el-button--primary {
    background: #f701ff;
    border-color: #f701ff;
    min-width: 120px;

    &:hover {
      opacity: 0.9;
    }
  }
}
</style> 